/*
 * Copyright (c) 2005-2006 FeySoft Inc. All Rights Reserved.
 */
package com.feyaSoft.plugin.util;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * This class is used to specified the number format. This will be used by
 * lucene to generate id.
 * 
 * TODO: archived ..... do not call this util again, please use DateUtil
 * 
 * @author fzhuang
 * @date Oct 27, 2005
 */
public class FormatUtil {

	private static final DecimalFormat formatter = new DecimalFormat(
			"0000000000");
	private static final DecimalFormat doubleFormatter = new DecimalFormat(
			"0000000000.00");
	private static final DecimalFormat dollarFormatter = new DecimalFormat(
			"##0.00");
	private static final SimpleDateFormat dateFormatter = new SimpleDateFormat(
			"yyyyMMddhhmm");

	private static final SimpleDateFormat dateFormatter2 = new SimpleDateFormat(
			"dd-MMM-yyyy HH:mm:ss");

	private static final SimpleDateFormat dateFormatter3 = new SimpleDateFormat(
			"yyyy-MM-dd");

	public static final SimpleDateFormat dateFormatter5 = new SimpleDateFormat(
			"MMM dd, yyyy");

	public static String numberPad(int n) {
		return formatter.format(n);
	}

	/**
	 * remove 0 from the front append.
	 * 
	 * @param in
	 * @return
	 */
	public static int unpadString(String in) {
		StringBuffer numberString = new StringBuffer();
		boolean startAppend = false;

		for (int i = 0; i < in.length(); i++) {
			char c = in.charAt(i);

			if (c != '0') {
				startAppend = true;
			}

			if (startAppend) {
				numberString.append(c);
			}
		}

		// check in is "00" or not
		if (in.equalsIgnoreCase("00")) {
			return 0;
		}

		return Integer.parseInt(numberString.toString());
	}

	public static String doublePad(double n) {
		return doubleFormatter.format(n);
	}

	public static String dollarPad(double n) {
		return dollarFormatter.format(n);
	}

	/**
	 * remove 0 from the front append.
	 * 
	 * @param in
	 * @return
	 */
	public static double unpadDoubleString(String in) {
		StringBuffer numberString = new StringBuffer();
		boolean startAppend = false;

		for (int i = 0; i < in.length(); i++) {
			char c = in.charAt(i);

			if (c != '0') {
				startAppend = true;
			}

			if (startAppend) {
				numberString.append(c);
			}
		}

		return Double.parseDouble(numberString.toString());
	}

	/**
	 * get date before n days
	 * 
	 * @param n
	 * @return
	 */
	public static Date getDate(int n) {

		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.DATE, -n);
		Date date = cal.getTime();

		return date;
	}

	/**
	 * get date before n days
	 * 
	 * @param n
	 * @return
	 */
	public static String getDateString(int n) {

		Calendar cal = new GregorianCalendar();
		cal.add(Calendar.DATE, -n);
		Date date = cal.getTime();
		String dateString = dateFormatter.format(date);
		return dateString;
	}

	public static Date string2Date(String in) throws Exception {
		return dateFormatter.parse(in);
	}

	public static Date string2Date2(String in) throws Exception {
		return dateFormatter2.parse(in);
	}

	public static Date string2Date3(String in) throws Exception {
		return dateFormatter3.parse(in);
	}

	public static String date2String(Date date) throws Exception {
		return dateFormatter2.format(date);
	}

	public static Date string2Date5(String in) throws Exception {
		return dateFormatter5.parse(in);
	}

	public static String date52String(Date date) {
		String result = "";
		try {
			result = dateFormatter5.format(date);
		} catch (Exception e) {}
		return result;
	}

}
